<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>搭建高可用mongodb集群—— 分片 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>搭建高可用mongodb集群—— 分片</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MongoDB/">MongoDB</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;按照<a href="https://hcldirgit.github.io/2017/08/28/MongoDB/16.%20%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E6%B7%B1%E5%85%A5%E5%89%AF%E6%9C%AC%E9%9B%86%E5%86%85%E9%83%A8%E6%9C%BA%E5%88%B6/" target="_blank" rel="external">搭建高可用mongodb集群—— 深入副本集内部机制</a>搭建后还有两个问题没有解决：</p>
<ul>
<li>从节点每个上面的数据都是对数据库全量拷贝，从节点压力会不会过大？</li>
<li>数据压力大到机器支撑不了的时候能否做到自动扩展？</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在系统早期，数据量还小的时候不会引起太大的问题，但是随着数据量持续增多，后续迟早会出现一台机器硬件瓶颈问题的。而mongodb主打的就是海量数据架构，他不能解决海量数据怎么行！不行！“分片”就用这个来解决这个问题。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;传统数据库怎么做海量数据读写？其实一句话概括：分而治之。上图看看就清楚了，如下 taobao岳旭强在infoq中提到的 架构图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E5%88%86%E7%89%87/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上图中有个TDDL，是taobao的一个数据访问层组件，他主要的作用是SQL解析、路由处理。根据应用的请求的功能解析当前访问的sql判断是在哪个业务数据库、哪个表访问查询并返回数据结果。具体如图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E5%88%86%E7%89%87/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说了这么多传统数据库的架构，那Nosql怎么去做到了这些呢？mysql要做到自动扩展需要加一个数据访问层用程序去扩展，数据库的增加、删除、备份还需要程序去控制。一但数据库的节点一多，要维护起来也是非常头疼的。不过mongodb所有的这一切通过他自己的内部机制就可以搞定！顿时石化了，这么牛X！还是上图看看mongodb通过哪些机制实现路由、分片：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E5%88%86%E7%89%87/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从图中可以看到有四个组件：mongos、config server、shard、replica set。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>mongos</strong>，数据库集群请求的入口，所有的请求都通过mongos进行协调，不需要在应用程序添加一个路由选择器，mongos自己就是一个请求分发中心，它负责把对应的数据请求请求转发到对应的shard服务器上。在生产环境通常有多mongos作为请求的入口，防止其中一个挂掉所有的mongodb请求都没有办法操作。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>config server</strong>，顾名思义为配置服务器，存储所有数据库元信息（路由、分片）的配置。mongos本身没有物理存储分片服务器和数据路由信息，只是缓存在内存里，配置服务器则实际存储这些数据。mongos第一次启动或者关掉重启就会从 config server 加载配置信息，以后如果配置服务器信息变化会通知到所有的 mongos 更新自己的状态，这样 mongos 就能继续准确路由。在生产环境通常有多个 config server 配置服务器，因为它存储了分片路由的元数据，这个可不能丢失！就算挂掉其中一台，只要还有存货， mongodb集群就不会挂掉。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>shard</strong>，这就是传说中的分片了。上面提到一个机器就算能力再大也有天花板，就像军队打仗一样，一个人再厉害喝血瓶也拼不过对方的一个师。俗话说三个臭皮匠顶个诸葛亮，这个时候团队的力量就凸显出来了。在互联网也是这样，一台普通的机器做不了的多台机器来做，如下图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E5%88%86%E7%89%87/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一台机器的一个数据表 Collection1 存储了 1T 数据，压力太大了！在分给4个机器后，每个机器都是256G，则分摊了集中在一台机器的压力。也许有人问一台机器硬盘加大一点不就可以了，为什么要分给四台机器呢？不要光想到存储空间，实际运行的数据库还有硬盘的读写、网络的IO、CPU和内存的瓶颈。在mongodb集群只要设置好了分片规则，通过mongos操作数据库就能自动把对应的数据操作请求转发到对应的分片机器上。在生产环境中分片的片键可要好好设置，这个影响到了怎么把数据均匀分到多个分片机器上，不要出现其中一台机器分了1T，其他机器没有分到的情况，这样还不如不分片！</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>replica set</strong>，上两节已经详细讲过了这个东东，怎么这里又来凑热闹！其实上图4个分片如果没有 replica set 是个不完整架构，假设其中的一个分片挂掉那四分之一的数据就丢失了，所以在高可用性的分片架构还需要对于每一个分片构建 replica set 副本集保证分片的可靠性。生产环境通常是 2个副本 + 1个仲裁。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说了这么多，还是来实战一下如何搭建高可用的mongodb集群：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先确定各个组件的数量，mongos 3个， config server 3个，数据分3片 shard server 3个，每个shard 有一个副本一个仲裁也就是 3 * 2 = 6 个，总共需要部署15个实例。这些实例可以部署在独立机器也可以部署在一台机器，我们这里测试资源有限，只准备了 3台机器，在同一台机器只要端口不同就可以，看一下物理部署图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E5%88%86%E7%89%87/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;架构搭好了，安装软件！</p>
<ol>
<li><p>准备机器，IP分别设置为： 192.168.0.136、192.168.0.137、192.168.0.138。</p>
</li>
<li><p>分别在每台机器上建立mongodb分片对应测试文件夹。</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#存放mongodb数据文件</span></div><div class="line">mkdir -p /data/mongodbtest</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#进入mongodb文件夹</span></div><div class="line"><span class="built_in">cd</span> /data/mongodbtest</div></pre></td></tr></table></figure>
<ol>
<li>下载mongodb的安装程序包</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.8.tgz</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#解压下载的压缩包</span></div><div class="line">tar xvzf mongodb-linux-x86_64-2.4.8.tgz</div></pre></td></tr></table></figure>
<ol>
<li>分别在每台机器建立mongos 、config 、 shard1 、shard2、shard3 五个目录。<br>因为mongos不存储数据，只需要建立日志文件目录即可。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立mongos目录</span></div><div class="line">mkdir -p /data/mongodbtest/mongos/<span class="built_in">log</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立config server 数据文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/config/data</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立config server 日志文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/config/<span class="built_in">log</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立config server 日志文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/mongos/<span class="built_in">log</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立shard1 数据文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/shard1/data</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立shard1 日志文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/shard1/<span class="built_in">log</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立shard2 数据文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/shard2/data</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立shard2 日志文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/shard2/<span class="built_in">log</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立shard3 数据文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/shard3/data</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#建立shard3 日志文件存放目录</span></div><div class="line">mkdir -p /data/mongodbtest/shard3/<span class="built_in">log</span></div></pre></td></tr></table></figure>
<ol>
<li>规划5个组件对应的端口号，由于一个机器需要同时部署 mongos、config server 、shard1、shard2、shard3，所以需要用端口进行区分。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个端口可以自由定义，在本文 mongos为 20000， config server 为 21000， shard1为 22001 ， shard2为22002， shard3为22003.</p>
<ol>
<li>在每一台服务器分别启动配置服务器。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongod --configsvr --dbpath /data/mongodbtest/config/data --port 21000 --logpath /data/mongodbtest/config/<span class="built_in">log</span>/config.log --fork</div></pre></td></tr></table></figure>
<ol>
<li>在每一台服务器分别启动mongos服务器。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongos --configdb 192.168.0.136:21000,192.168.0.137:21000,192.168.0.138:21000 --port 20000 --logpath  /data/mongodbtest/mongos/<span class="built_in">log</span>/mongos.log --fork</div></pre></td></tr></table></figure>
<ol>
<li>配置各个分片的副本集。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#在每个机器里分别设置分片1服务器及副本集shard1</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongod --shardsvr --replSet shard1 --port 22001 --dbpath /data/mongodbtest/shard1/data --logpath /data/mongodbtest/shard1/<span class="built_in">log</span>/shard1.log --fork --nojournal --oplogSize 10</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了快速启动并节约测试环境存储空间，这里加上 nojournal 是为了关闭日志信息，在我们的测试环境不需要初始化这么大的redo日志。同样设置 oplogsize是为了降低 local 文件的大小，oplog是一个固定长度的 capped collection,它存在于”local”数据库中,用于记录Replica Sets操作日志。注意，这里的设置是为了测试！</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#在每个机器里分别设置分片2服务器及副本集shard2</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongod --shardsvr --replSet shard2 --port 22002 --dbpath /data/mongodbtest/shard2/data --logpath /data/mongodbtest/shard2/<span class="built_in">log</span>/shard2.log --fork --nojournal --oplogSize 10</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#在每个机器里分别设置分片3服务器及副本集shard3</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongod --shardsvr --replSet shard3 --port 22003 --dbpath /data/mongodbtest/shard3/data --logpath /data/mongodbtest/shard3/<span class="built_in">log</span>/shard3.log --fork --nojournal --oplogSize 10</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;分别对每个分片配置副本集，深入了解副本集参考本系列前几篇文章。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;任意登陆一个机器，比如登陆192.168.0.136，连接mongodb</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#设置第一个分片副本集</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:22001</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#使用admin数据库</span></div><div class="line">use admin</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#定义副本集配置</span></div><div class="line">config = &#123; _id:<span class="string">"shard1"</span>, members:[</div><div class="line"> &#123;_id:0,host:<span class="string">"192.168.0.136:22001"</span>&#125;,</div><div class="line"> &#123;_id:1,host:<span class="string">"192.168.0.137:22001"</span>&#125;,</div><div class="line"> &#123;_id:2,host:<span class="string">"192.168.0.138:22001"</span>,arbiterOnly:<span class="literal">true</span>&#125;</div><div class="line"> ]</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#初始化副本集配置</span></div><div class="line">rs.initiate(config);</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#设置第二个分片副本集</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:22002</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#使用admin数据库</span></div><div class="line">use admin</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#定义副本集配置</span></div><div class="line">config = &#123; _id:<span class="string">"shard2"</span>, members:[</div><div class="line"> &#123;_id:0,host:<span class="string">"192.168.0.136:22002"</span>&#125;,</div><div class="line"> &#123;_id:1,host:<span class="string">"192.168.0.137:22002"</span>&#125;,</div><div class="line"> &#123;_id:2,host:<span class="string">"192.168.0.138:22002"</span>,arbiterOnly:<span class="literal">true</span>&#125;</div><div class="line"> ]</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#初始化副本集配置</span></div><div class="line">rs.initiate(config);</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#设置第三个分片副本集</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:22003</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#使用admin数据库</span></div><div class="line">use admin</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#定义副本集配置</span></div><div class="line">config = &#123; _id:<span class="string">"shard3"</span>, members:[</div><div class="line"> &#123;_id:0,host:<span class="string">"192.168.0.136:22003"</span>&#125;,</div><div class="line"> &#123;_id:1,host:<span class="string">"192.168.0.137:22003"</span>&#125;,</div><div class="line"> &#123;_id:2,host:<span class="string">"192.168.0.138:22003"</span>,arbiterOnly:<span class="literal">true</span>&#125;</div><div class="line"> ]</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#初始化副本集配置</span></div><div class="line">rs.initiate(config);</div></pre></td></tr></table></figure>
<ol>
<li>目前搭建了mongodb配置服务器、路由服务器，各个分片服务器，不过应用程序连接到 mongos 路由服务器并不能使用分片机制，还需要在程序里设置分片配置，让分片生效。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#连接到mongos</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:20000</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#使用admin数据库</span></div><div class="line">user admin</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#串联路由服务器与分配副本集1</span></div><div class="line">db.runCommand( &#123; addshard : <span class="string">"shard1/192.168.0.136:22001,192.168.0.137:22001,192.168.0.138:22001"</span>&#125;);</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如里shard是单台服务器，用 db.runCommand( { addshard : “[: ]” } )这样的命令加入，如果shard是副本集，用db.runCommand( { addshard : “replicaSetName/[:port][,serverhostname2[:port],…]” });这样的格式表示 。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#串联路由服务器与分配副本集2</span></div><div class="line">db.runCommand( &#123; addshard : <span class="string">"shard2/192.168.0.136:22002,192.168.0.137:22002,192.168.0.138:22002"</span>&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#串联路由服务器与分配副本集3</span></div><div class="line">db.runCommand( &#123; addshard : <span class="string">"shard3/192.168.0.136:22003,192.168.0.137:22003,192.168.0.138:22003"</span>&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#查看分片服务器的配置</span></div><div class="line">db.runCommand( &#123; listshards : 1 &#125; );</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容输出</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line"> <span class="string">"shards"</span> : [</div><div class="line"> &#123;</div><div class="line"> <span class="string">"_id"</span> : <span class="string">"shard1"</span>,</div><div class="line"> <span class="string">"host"</span> : <span class="string">"shard1/192.168.0.136:22001,192.168.0.137:22001"</span></div><div class="line"> &#125;,</div><div class="line"> &#123;</div><div class="line"> <span class="string">"_id"</span> : <span class="string">"shard2"</span>,</div><div class="line"> <span class="string">"host"</span> : <span class="string">"shard2/192.168.0.136:22002,192.168.0.137:22002"</span></div><div class="line"> &#125;,</div><div class="line"> &#123;</div><div class="line"> <span class="string">"_id"</span> : <span class="string">"shard3"</span>,</div><div class="line"> <span class="string">"host"</span> : <span class="string">"shard3/192.168.0.136:22003,192.168.0.137:22003"</span></div><div class="line"> &#125;</div><div class="line"> ],</div><div class="line"> <span class="string">"ok"</span> : 1</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为192.168.0.138是每个分片副本集的仲裁节点，所以在上面结果没有列出来。</p>
<ol>
<li>目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了，但我们的目的是希望插入数据，数据能够自动分片，就差那么一点点，一点点。。。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;连接在mongos上，准备让指定的数据库、指定的集合分片生效。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#指定testdb分片生效</span></div><div class="line">db.runCommand( &#123; enablesharding :<span class="string">"testdb"</span>&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#指定数据库里需要分片的集合和片键</span></div><div class="line">db.runCommand( &#123; shardcollection : <span class="string">"testdb.table1"</span>,key : &#123;id: 1&#125; &#125; )</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我们设置testdb的 table1 表需要分片，根据 id 自动分片到 shard1 ，shard2，shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片！</p>
<ol>
<li>测试分片配置结果。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#连接mongos服务器</span></div><div class="line">/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:20000</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#使用testdb</span></div><div class="line">use testdb;</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#插入测试数据</span></div><div class="line"><span class="keyword">for</span> (var i = 1; i &lt;= 100000; i++)</div><div class="line">db.table1.save(&#123;id:i,<span class="string">"test1"</span>:<span class="string">"testval1"</span>&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#查看分片情况如下，部分无关信息省掉了</span></div><div class="line">db.table1.stats();</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line">&#123;</div><div class="line"> <span class="string">"sharded"</span> : <span class="literal">true</span>,</div><div class="line"> <span class="string">"ns"</span> : <span class="string">"testdb.table1"</span>,</div><div class="line"> <span class="string">"count"</span> : 100000,</div><div class="line"> <span class="string">"numExtents"</span> : 13,</div><div class="line"> <span class="string">"size"</span> : 5600000,</div><div class="line"> <span class="string">"storageSize"</span> : 22372352,</div><div class="line"> <span class="string">"totalIndexSize"</span> : 6213760,</div><div class="line"> <span class="string">"indexSizes"</span> : &#123;</div><div class="line"> <span class="string">"_id_"</span> : 3335808,</div><div class="line"> <span class="string">"id_1"</span> : 2877952</div><div class="line"> &#125;,</div><div class="line"> <span class="string">"avgObjSize"</span> : 56,</div><div class="line"> <span class="string">"nindexes"</span> : 2,</div><div class="line"> <span class="string">"nchunks"</span> : 3,</div><div class="line"> <span class="string">"shards"</span> : &#123;</div><div class="line"> <span class="string">"shard1"</span> : &#123;</div><div class="line"> <span class="string">"ns"</span> : <span class="string">"testdb.table1"</span>,</div><div class="line"> <span class="string">"count"</span> : 42183,</div><div class="line"> <span class="string">"size"</span> : 0,</div><div class="line"> ...</div><div class="line"> <span class="string">"ok"</span> : 1</div><div class="line"> &#125;,</div><div class="line"> <span class="string">"shard2"</span> : &#123;</div><div class="line"> <span class="string">"ns"</span> : <span class="string">"testdb.table1"</span>,</div><div class="line"> <span class="string">"count"</span> : 38937,</div><div class="line"> <span class="string">"size"</span> : 2180472,</div><div class="line"> ...</div><div class="line"> <span class="string">"ok"</span> : 1</div><div class="line"> &#125;,</div><div class="line"> <span class="string">"shard3"</span> : &#123;</div><div class="line"> <span class="string">"ns"</span> : <span class="string">"testdb.table1"</span>,</div><div class="line"> <span class="string">"count"</span> :18880,</div><div class="line"> <span class="string">"size"</span> : 3419528,</div><div class="line"> ...</div><div class="line"> <span class="string">"ok"</span> : 1</div><div class="line"> &#125;</div><div class="line"> &#125;,</div><div class="line"> <span class="string">"ok"</span> : 1</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以看到数据分到3个分片，各自分片数量为： shard1 “count” : 42183，shard2  “count” : 38937，shard3 “count” : 18880。已经成功了！不过分的好像不是很均 匀，所以这个分片还是很有讲究的，后续再深入讨论。</p>
<ol>
<li>java程序调用分片集群，因为我们配置了三个mongos作为入口，就算其中哪个入口挂掉了都没关系，使用集群客户端程序如下：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line">public class TestMongoDBShards &#123;</div><div class="line"> </div><div class="line"> public static void main(String[] args) &#123;</div><div class="line"> </div><div class="line"> try &#123;</div><div class="line"> List&lt;ServerAddress&gt; addresses = new ArrayList&lt;ServerAddress&gt;();</div><div class="line"> ServerAddress address1 = new ServerAddress(<span class="string">"192.168.0.136"</span> , 20000);</div><div class="line"> ServerAddress address2 = new ServerAddress(<span class="string">"192.168.0.137"</span> , 20000);</div><div class="line"> ServerAddress address3 = new ServerAddress(<span class="string">"192.168.0.138"</span> , 20000);</div><div class="line"> addresses.add(address1);</div><div class="line"> addresses.add(address2);</div><div class="line"> addresses.add(address3);</div><div class="line"> </div><div class="line"> MongoClient client = new MongoClient(addresses);</div><div class="line"> DB db = client.getDB( <span class="string">"testdb"</span> );</div><div class="line"> DBCollection coll = db.getCollection( <span class="string">"table1"</span> );</div><div class="line"> </div><div class="line"> BasicDBObject object = new BasicDBObject();</div><div class="line"> object.append( <span class="string">"id"</span> , 1);</div><div class="line"> </div><div class="line"> DBObject dbObject = coll.findOne(object);</div><div class="line"> </div><div class="line"> System. out .println(dbObject);</div><div class="line"> </div><div class="line"> &#125; catch (Exception e) &#123;</div><div class="line"> e.printStackTrace();</div><div class="line"> &#125;</div><div class="line"> &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;整个分片集群搭建完了，思考一下我们这个架构是不是足够好呢？其实还有很多地方需要优化，比如我们把所有的仲裁节点放在一台机器，其余两台机器承担了全部读写操作，但是作为仲裁的192.168.0.138相当空闲。让机器3 192.168.0.138多分担点责任吧！架构可以这样调整，把机器的负载分的更加均衡一点，每个机器既可以作为主节点、副本节点、仲裁节点，这样压力就会均衡很多了，如图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E5%88%86%E7%89%87/06.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当然生产环境的数据远远大于当前的测试数据，大规模数据应用情况下我们不可能把全部的节点像这样部署，硬件瓶颈是硬伤，只能扩展机器。要用好mongodb还有很多机制需要调整，不过通过这个东东我们可以快速实现高可用性、高扩展性，所以它还是一个非常不错的Nosql组件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再看看我们使用的mongodb java 驱动客户端 MongoClient(addresses)，这个可以传入多个mongos 的地址作为mongodb集群的入口，并且可以实现自动故障转移，但是负载均衡做的好不好呢？打开源代码查看：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8mongodb%E9%9B%86%E7%BE%A4%E2%80%94%E2%80%94%20%E5%88%86%E7%89%87/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它的机制是选择一个ping 最快的机器来作为所有请求的入口，如果这台机器挂掉会使用下一台机器。那这样。。。。肯定是不行的！万一出现双十一这样的情况所有请求集中发送到这一台机器，这台机器很有可能挂掉。一但挂掉了，按照它的机制会转移请求到下台机器，但是这个压力总量还是没有减少啊！下一台还是可能崩溃，所以这个架构还有漏洞！不过这个文章已经太长了，后续解决吧。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参考：<a href="http://docs.mongodb.org/manual/core/sharding-introduction/" target="_blank" rel="external">http://docs.mongodb.org/manual/core/sharding-introduction/</a></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MongoDB/17. 搭建高可用mongodb集群—— 分片/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
